package com.glyxybxhtxt.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Author:wangzh
 * Date: 2020/12/5 1:53
 * Version: 1.0
 */
@Configuration
public class CrosConfig implements WebMvcConfigurer {

    @Value("${upload.picture_src}")
    private String pictureSavePath;
    
    /**
     * 解决跨域问题
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://yiban.glmc.edu.cn","http://localhost/bx","http://localhost:8090")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                .maxAge(3600);
    }

    /**
     * 资源映射路径
     * 请求该 URI 则去 pictureSavePath 路径下查找资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 值班图片路径映射
        registry.addResourceHandler("/duty-picture/**").addResourceLocations("file:" + pictureSavePath);
    }
}
